/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import bean.Barang;
import java.io.*;
import java.sql.*;
import java.util.logging.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author Rosi
 */
public class SimpanDataBarang extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String nomor = request.getParameter("nomor");
        String jumlah = request.getParameter("jumlah");
        String berat = request.getParameter("berat");
        String jenis = request.getParameter("jenis");
        String biaya = request.getParameter("biaya");

//        String cancel = request.getParameter("batal");
//        String next = request.getParameter("next");

        Barang barang = new Barang();
        barang.setNoBarang(nomor);
        barang.setJumlahBarang(jumlah);
        barang.setBeratbarang(berat);
        barang.setJenisBarang(jenis);
        barang.setBiayaKirim(biaya);


//        if (barang.getNoBarang()==null) {
//            RequestDispatcher req = request.getRequestDispatcher("FrameUtama.jsp");
//            req.forward(request, response);
//        } else {
            try {
                Connection conn = koneksi.Koneksi.open();
                Statement state = conn.createStatement();
                String query = "insert into data_barang(no_barang, berat_barang, jumlah_barang, jenis_barang, biaya_kirim) "
                        + "values ('" + barang.getNoBarang() + "','"
                        + barang.getBeratbarang() + "','"
                        + barang.getJumlahBarang() + "','"
                        + barang.getJenisBarang() + "','"
                        + barang.getBiayaKirim() +"')";
                state.executeUpdate(query);
                RequestDispatcher req = request.getRequestDispatcher("formSimpanDataKonsumen.jsp");
                req.forward(request, response);
                out.close();
            } catch (SQLException ex) {
                Logger.getLogger(SimpanDataBarang.class.getName()).log(Level.SEVERE, null, ex);
            }
//        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
